package org.testis.webautomationmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.data.WebAutomationModuleDataNode;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.webautomationmodule.ui.WebAutomationDecorator;
import org.testis.webautomationmodule.ui.WebAutomationTestStepBuilder;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.webautomationmodule.ui.UIElementNotEditableException;
import org.testis.webautomationmodule.ui.UIElementNotFoundException;
import org.testis.webautomationmodule.ui.UIElementNotVisibleException;
import org.testis.webautomationmodule.ui.UIFormFieldDecorator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;


/**
 * A action can be used for get value of web ui objects
 * @author nurhat
 */
public class GetAction extends AtofModuleAction
{

    @Override
    public boolean operateRequest(AtofModuleService request)
    {
        try {
            WebAutomationModuleProviderService service =
                (WebAutomationModuleProviderService) request;
            WebAutomationModuleDataObject dataObj =
                (WebAutomationModuleDataObject) service.getDataObj();
            if (request.getDataObj() == null) {
                throw new TestStepException(
                    TestStepResultType.MISTAKE,
                    "Web Automation Get  Test failed. A Null WebAutomationDataObject recieved....");
            }

            LogSenderService.putMessageOnQueue(
                "WebAutomationGetAction",
                AtofLogLevel.INFO,
                "Web Automation Get Test step started....",
                dataObj.getMessageId());
            if (service.openWindow()) {
                if (runDecorator(service,
                                 dataObj.getLoginDataList())) {
                    if (runGet(service,
                               dataObj.getPageObjectsDataList())) {
                        return true;
                    }
                    else {
                        LogSenderService.putMessageOnQueue(
                            "WebAutomationGetAction",
                            AtofLogLevel.INFO,
                            "Web Automation get test failed...",
                            dataObj.getMessageId());
                    }
                }
                else {
                    LogSenderService.putMessageOnQueue(
                        "WebAutomationGetAction",
                        AtofLogLevel.INFO,
                        "Web Automation get test failed...",
                        dataObj.getMessageId());
                }
            }
        } catch (UIElementNotEditableException ex) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Get Test Step failed..." + ex.toString());
        } catch (UIElementNotFoundException ex) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Get Test Step failed..." +ex.toString());
        }  catch (UIElementNotVisibleException ex) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation Get Test Step failed..." +ex.toString());
        }catch (TestStepException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new TestStepException(
                TestStepResultType.FAULT,
                "Web Automation Get Test Step failed..." +ex.toString());
        }
        return false;

    }


    private boolean runDecorator(WebAutomationModuleProviderService request,
                                 ArrayList<WebAutomationModuleDataNode> list)
    {
        WebAutomationTestStepBuilder ts =
            new WebAutomationTestStepBuilder(request.getAutomate());
        WebAutomationDecorator decorator = ts.createTestStep(list,
                                                             request.getDataObj().
                                                             getMessageId());
        if (decorator != null) {

            if (decorator.operate()) {

                return true;
            }

        }
        else {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "WebAutomation test failed at WebAutomationGetAction...Could not create object decorator...");
        }
        return false;
    }


    private boolean runGet(WebAutomationModuleProviderService request,
                           ArrayList<WebAutomationModuleDataNode> list)
    {

        WebAutomationTestStepBuilder ts =
            new WebAutomationTestStepBuilder(request.getAutomate());
        WebAutomationDecorator decorator = ts.createTestStep(list,
                                                             request.getDataObj().
                                                             getMessageId());
        if (decorator != null) {
            WebAutomationModuleDataObject dataObj =
                (WebAutomationModuleDataObject) request.getDataObj();

            boolean result = decorator.get();
            if (result) {
                while (decorator != null) {
                    if (decorator instanceof UIFormFieldDecorator) {
                        dataObj.getUiObjectValueList().put(((UIFormFieldDecorator) decorator).
                                                           getAbstractId(),
                                                           ((UIFormFieldDecorator) decorator).
                                                           getValue());
                    }
                    decorator = decorator.getNextUIObjectDecorator();
                }
            /*    Iterator iter = dataObj.getUiObjectValueList().entrySet().
            iterator();
            while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            System.out.println(entry.getKey() + " " + entry.getValue());
            }*/
            }
            return result;

        }
        else {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Web Automation get test failed at WebAutomationGetAction...Could not create object decorator...");

        }

    }
}
